<script setup lang="ts">
import { useUrlSearchParams } from '@vueuse/core'
import { onMounted, ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { loginApi } from '@/apis/common/loginApi'
import { useLoginStore } from '@/stores/loginStore'

const loginType = ref()
const authCode = ref()
const router = useRouter()
const route = useRoute()
onMounted(() => {
  const type = route.query.type
  loginType.value = route.query.loginType
  const params = useUrlSearchParams('history')
  authCode.value = params.authCode

  if (type === 'bind') {
    bind()
  }
  else if (type === 'login') {
    login()
  }
})

async function bind() {
  await loginApi.bindAccount({
    thirdCode: authCode.value,
    loginType: loginType.value,
  })
  ElMessage.success('账号绑定成功')
  router.push('/home/userInfo')
}

const loginStore = useLoginStore()
async function login() {
  const data = await loginApi.login({
    authCode: authCode.value,
    loginType: loginType.value,
  })
  loginStore.login(data)
  ElMessage.success('登录成功')
  router.push('/')
}
</script>

<template>
  <div>正在处理...</div>
</template>
